package com.ml.shs.service;

import com.ml.shs.entity.Classify;
import com.ml.shs.repository.ClassifyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.UUID;

@Service
public class ClassifyService {

    @Autowired
    public ClassifyRepository classifyRepository;

    public List<Classify> getList() {
        return classifyRepository.findAll();
    }

    public void save(Classify classify) {
        setClassifyId(classify);
        classifyRepository.save(classify);
    }

    public void setClassifyId(Classify classify) {
        if (classify != null) {
            if (classify.getLid() == null) {
                classify.setLid(UUID.randomUUID().toString());
            }
            List<Classify> children = classify.getChildren();
            if (children != null && children.size() > 0) {
                children.forEach(item -> {
                    item.setLevel("1");
                    setClassifyId(item);
                });
            }
        }
    }
}
